package com.lxh.student.controller;

import com.lxh.student.entity.ResponseResult;
import com.lxh.student.entity.SwaggerTestUser;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author lxh
 * @date 2023/4/8 15:39
 */

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping
    public ResponseResult test(){
        return new ResponseResult(200,"测试！");
    }

    //有test1权限才能够访问
    @GetMapping("test1")
    @PreAuthorize("hasAnyAuthority('test1')")//也能写在类上
    public ResponseResult testWithTest1(){
        return new ResponseResult(200,"有test1权限，测试！");
    }
    //有test2权限才能够访问
    @GetMapping("test2")
    @PreAuthorize("hasAnyAuthority('test4')")//也能写在类上
    public ResponseResult testWithTest2(){
        return new ResponseResult(200,"有test2权限，测试！");
    }
    //每一个接口方法上都写死权限

    @ApiOperation("测试接口1")
    @PostMapping("/swagger1")
    public String swagger1(@ApiParam(value = "swagger测试用户对象",required = true) @RequestBody SwaggerTestUser swaggerTestUser){
        return "hello," + swaggerTestUser.getName() +"，你今年"+swaggerTestUser.getAge()+"岁"+ ",welcome to springboot swagger3！";
    }

    @ApiOperation("测试接口3")
    @PostMapping("/swagger3")
    @ApiImplicitParams({@ApiImplicitParam(name = "swaggerTestUser",value = "用户",required = true,paramType = "query")})
    public String swagger3(@RequestBody SwaggerTestUser swaggerTestUser){
        return "hello," + swaggerTestUser.getName() +"，你今年"+swaggerTestUser.getAge()+"岁"+ ",welcome to springboot swagger3！";
    }

    @ApiOperation("测试接口4")
    @PostMapping("/swagger4")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query"),
                        @ApiImplicitParam(name = "age", value = "年龄",required = true,paramType = "query")
    })
    public String swagger4(String name,Integer age){
        return "hello," + name +"，你今年"+age+"岁"+ ",welcome to springboot swagger3！";
    }

    @ApiOperation("测试接口5")
    @PostMapping("/swagger5")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query")})
    public String swagger5(String name){
        return "hello," + name +"，你今年"+99+"岁"+ ",welcome to springboot swagger3！";
    }

    @ApiOperation("测试接口2")
    @GetMapping("/swagger2")
    public String swagger2(){
        return "hello! GetMapping swagger3!";
    }
}
